/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = bytebuff_memstr.c                                  */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/13 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void case1(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"abcdef", strlen("abcdef"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "d", strlen("d"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 3);

    bytebuff_free(&p_packet);

    return;
}

static void case2(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"abcdef", strlen("abcdef"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "h", strlen("h"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_packet);

    return;
}

static void case3(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"abcdef", strlen("abcdef"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "bc", strlen("bc"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 1);

    bytebuff_free(&p_packet);

    return;
}

static void case4(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"abcdef", strlen("abcdef"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "ef", strlen("ef"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 4);

    bytebuff_free(&p_packet);

    return;
}

static void case5(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"abcdef", strlen("abcdef"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "fg", strlen("fg"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_packet);

    return;
}

static void case6(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"123456", strlen("123456"));
    bytebuff_add(p_packet, (char *)"abcdef", strlen("abcdef"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "6a", strlen("6a"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 5);

    bytebuff_free(&p_packet);

    return;
}

static void case7(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"123456", strlen("123456"));
    bytebuff_add(p_packet, (char *)"a", strlen("a"));
    bytebuff_add(p_packet, (char *)"b", strlen("b"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "6ab", strlen("6ab"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 5);

    bytebuff_free(&p_packet);

    return;
}

static void case8(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"123456", strlen("123456"));
    bytebuff_add(p_packet, (char *)"a", strlen("a"));
    bytebuff_add(p_packet, (char *)"b", strlen("b"));

    i_ret = bytebuff_memstr(p_packet, 0, 0, "6a", strlen("6a"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 5);

    bytebuff_free(&p_packet);

    return;
}

static void case9(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"123456", strlen("123456"));
    bytebuff_add(p_packet, (char *)"a", strlen("a"));
    bytebuff_add(p_packet, (char *)"b", strlen("b"));

    i_ret = bytebuff_memstr(p_packet, 5, 0, "6a", strlen("6a"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_pos, 5);

    bytebuff_free(&p_packet);

    return;
}

static void case10(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0, i_pos = 0;

    p_packet = bytebuff_creat();

    bytebuff_add(p_packet, (char *)"123456", strlen("123456"));
    bytebuff_add(p_packet, (char *)"a", strlen("a"));
    bytebuff_add(p_packet, (char *)"b", strlen("b"));

    i_ret = bytebuff_memstr(p_packet, 6, 0, "6a", strlen("6a"), &i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_packet);

    return;
}

int add_suite_bytebuff_memstr(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frcomm bytebuff_memstr()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);
    CU_add_test(pSuite, "case7", case7);
    CU_add_test(pSuite, "case8", case8);
    CU_add_test(pSuite, "case9", case9);
    CU_add_test(pSuite, "case10", case10);
    

    return FR_SUCC;
}
